package com.shuai.orderfood.common;

import lombok.Data;

@Data
public class Result<T> {
    private Integer code;

    private String message;

    //data可以声明称Object类型，但是这样到时候还需要强转，因此直接使用泛型，
    //到时候data是User类型就会返回User类型，是Vip类型就会返回Vip类型。
    private T data;

    private Result() {

    }

    private Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    private Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //不携带数据的成功方法
    public static <T> Result<T> success() {
        return new Result<>(HttpStatusCodeEnum.SUCCESS.getCode(), HttpStatusCodeEnum.SUCCESS.getMessage());
    }

    //携带数据的成功方法
    public static <T> Result<T> success(T data) {
        return new Result<>(HttpStatusCodeEnum.SUCCESS.getCode(), HttpStatusCodeEnum.SUCCESS.getMessage(), data);
    }

    //定制响应码的成功方法，不携带数据
    public static <T> Result<T> ok(Integer code, String message) {
        return new Result<>(code, message);
    }

    //定制响应码的成功方法，携带数据
    public static <T> Result<T> ok(Integer code, String message, T data) {
        return new Result<>(code, message, data);
    }

    //失败方法
    public static <T> Result<T> fail() {
        return new Result<>(HttpStatusCodeEnum.FAIL.getCode(), HttpStatusCodeEnum.FAIL.getMessage());
    }

    //定制响应码的失败方法
    public static <T> Result<T> fail(Integer code, String message) {
        return new Result<>(code, message);
    }

    //通用的方法，不携带数据
    public static <T> Result<T> method(HttpStatusCodeEnum httpStatusCodeEnum) {
        return new Result<>(httpStatusCodeEnum.getCode(), httpStatusCodeEnum.getMessage());
    }
}
